package com.yangye.springclouddemomqtt.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yangye.springclouddemomqtt.gateway.MqttGateway;
import com.yangye.springclouddemomqtt.model.BaseOutCommand;
import com.yangye.springclouddemomqtt.model.IotMessage;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yangye
 * @date 2019/11/4 0004
 */
@RestController
public class MqttSenderController {

	@Autowired
	private MqttGateway mqttGateway;

	private ObjectMapper objectMapper = new ObjectMapper();

	@GetMapping("/mqtt/send")
	String send(String data, Integer cmd) throws JsonProcessingException {
		BaseOutCommand<TestBody> baseOutCommand = new BaseOutCommand(cmd, "testDeviceCode");
		TestBody body = new TestBody();
		body.setName(data);
		baseOutCommand.setData(body);
		mqttGateway.sendToMqtt("/robots-iot/center/", objectMapper.writeValueAsString(baseOutCommand));
		return "ok";
	}

	@Data
	class TestBody {
		String name;
	}

}
